//
//  AppDelegate.swift
//  webo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        registerNotification()
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
//        let ison = HMUserAccountViewModel.shared.userLogin
//        print(ison)
//        let navi = UINavigationController(rootViewController: HMComposeViewController())
        self.window?.rootViewController = HMUserAccountViewModel.shared.userLogin ? HMWelcomeViewController() : HMTabBarController()
        self.window?.makeKeyAndVisible()
        
    
        return true
    }
    
   private func registerNotification(){
    
    NotificationCenter.default.addObserver(self, selector: #selector(chooseRootViewController(noti:)), name: NSNotification.Name(chooseNotificationLoginOn), object: nil)
    
    }
    
   //为了便于维护keywindow的主控制器，使用发通知的方式
   @objc private func chooseRootViewController(noti: NSNotification){
    
    UIApplication.shared.keyWindow?.rootViewController = (noti.object == nil) ? HMTabBarController() : HMWelcomeViewController()
    
    
    }
    func applicationDidEnterBackground(_ application: UIApplication) {
HMStatusDAL.clearCacheStatus()
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

